package com.example.rdcproject.ving.notes.dao;


import com.example.rdcproject.ving.notes.entity.User;
import com.example.rdcproject.ving.notes.entity.Word;
import com.example.rdcproject.ving.notes.entity.WordBook;
import com.example.rdcproject.ving.notes.service.ConnectionPool;
import com.example.rdcproject.ving.notes.util.DBUtils;
import com.example.rdcproject.ving.notes.util.LearnWordUtil;
import com.example.rdcproject.ving.notes.util.PasswordUtils;
import com.example.rdcproject.ving.notes.view.User.UserInteractionView;

import java.sql.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;


public class UserDao {

    private Connection connection;

    private PreparedStatement statement;

    //用户登录
    public boolean login(String account, String password) throws SQLException {
          User user = User.getInstance();
          user.setNickname(account);
          user.setPassword(password);
        ConnectionPool connectionPool = null;
        try {

            connectionPool = ConnectionPool.getInstance();
            connection = connectionPool.getConnection();

            //定义SQL查询语句
            String sql = "select * from user where user_nickname = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, account);

            //执行查询
            ResultSet rs = statement.executeQuery();
            //判断查询结果
            if(rs.next()){
                // 获取数据库中存储的加密后的密码
                String dbPassword = rs.getString("user_password");

                // 对输入的密码进行加密
                if(PasswordUtils.checkPassword(password,dbPassword)){
                    System.out.println("登录成功");
                    initUser(user);
                    UserInteractionView view = new UserInteractionView();
                    return true;
                }else {
                    System.out.println("密码错误");
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            connectionPool.releaseConnection(connection);
            DBUtils.close(statement);

        }
        System.out.println("该用户不存在");
        return  false;
    }

    public void initUser(User user) {
        ConnectionPool connectionPool = null;
        try {

            connectionPool = ConnectionPool.getInstance();
            connection = connectionPool.getConnection();

            //定义SQL查询语句
            String sql = "select user_dailygoal,user_wordCount, user_currentWordIndex from user where user_nickname = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getNickname());

            //执行查询
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                int goal = rs.getInt("user_dailygoal");
                int wordCount = rs.getInt("user_wordCount");
                int currentWordIndex =rs.getInt("user_currentWordIndex");
                user.setWordCount(wordCount);
                user.setDailyGoal(goal);
                user.setCurrentWordIndex(currentWordIndex);
            }



        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   //用户注册
    public boolean register(String account,String outputPassword,String againPassword) throws SQLException {
        User user = new User();

        System.out.print("请输入用户名：");
        user.setNickname(account);
        System.out.print("请输入密码：");
        String password = outputPassword;
        user.setPassword(PasswordUtils.hashPassword(password));

            System.out.print("请再次输入密码：");
            String confirmPassword = againPassword;
            if (!password.equals(confirmPassword)) {
                System.out.println("两次密码输入不一致！");
                return false;
            }
        ConnectionPool connectionPool = null;

        try {
            connectionPool = ConnectionPool.getInstance();
            connection = connectionPool.getConnection();

            //定义SQL插入语句
            String sql = "insert into user(user_nickname,user_password) values (?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getNickname());
            statement.setString(2, user.getPassword());

            //执行查询
            statement.executeUpdate();
            System.out.println("注册成功");
        } catch (Exception e) {
            e.printStackTrace();

        } finally{
            connectionPool.releaseConnection(connection);
            DBUtils.close(statement);
        }
            return true;
    }



   //添加词书
    public WordBook addBook(String name) throws Exception {
        WordBook book = WordBook.getInstance();
        ConnectionPool connectionPool = null;

        try {
            connectionPool = ConnectionPool.getInstance();
            connection = connectionPool.getConnection();

            //通过想要词书名字找到从
            String sql = "select* from  word_book where name =?";
            statement = connection.prepareStatement(sql);

            statement.setString(1, name);

            ResultSet resultSet = statement.executeQuery();
            String bookName = resultSet.getString("name");
            if (bookName == null){
                System.out.println("未找到相应的词书，添加失败！");
                return null;
            }else {
                String sql2 = "select * from"+bookName+"";
                Statement statement2 = connection.createStatement();
                ResultSet resultSet2 = statement2.executeQuery(sql2);
                //结果集解析
                while (resultSet2.next()) {
                    Word word = new Word();
                  word.setWord(resultSet2.getString("word"));
                  word.setMeaning(resultSet2.getString("meaning"));
                    book.addWord(word);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("添加词书成功！");
        return  book;
    }

    //获取已添加的词书
    public static List getWordList(){
        User user = User.getInstance();
        LearnWordUtil learnWordUtil = LearnWordUtil.getInstance();


        ConnectionPool connectionPool = null;
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connectionPool = ConnectionPool.getInstance();
            connection = connectionPool.getConnection();

            // 开始事务
            connection.setAutoCommit(false);

            String sql ="select * from "+user.getNickname()+"WordBook";
            statement = connection.prepareStatement(sql);

            ResultSet rs = statement.executeQuery();
            while (rs.next()){
                Word word = new Word();
               String english = rs.getString("word");
               String meaning = rs.getString("meaning");
               word.setWord(english);
               word.setMeaning(meaning);
               learnWordUtil.getWordList().add(word);
            }


        }catch (Exception e) {
            e.printStackTrace();
        }
        return  learnWordUtil.getWordList();
    }

 public void updateWord(User user){
     ConnectionPool connectionPool = null;
     Connection connection = null;
     Statement statement = null;
     try {
         connectionPool = ConnectionPool.getInstance();
         connection = connectionPool.getConnection();

         // 开始事务
         connection.setAutoCommit(false);

         String nickname = user.getNickname();
         int wordcount = user.getWordCount();
         int currentWordIndex = user.getCurrentWordIndex();

         String sql ="update user set user_wordCount = "+wordcount+" where user_nickname = "+"'"+nickname+"'";
         String sql2 ="update user set user_currentWordIndex = "+ currentWordIndex+" where user_nickname = "+"'"+nickname+"'";

         statement = connection.createStatement();

         int rs = statement.executeUpdate(sql);
         int rs2 = statement.executeUpdate(sql2);
         if (rs>0&&rs2>0) {
             System.out.println("更新用户数据成功");
         }else {
             System.out.println("更新用户数据失败！");
         }


         connection.commit();
     }catch (Exception e) {
         e.printStackTrace();
     }

 }



}
